﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.Data;
using System.Windows.Threading;

namespace HITGPA
{
    using System.Windows.Forms; // NotifyIcon control
    using System.Drawing;
    using HITGPA.Properties; // Icon

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        Regex regex = new Regex(@"<TR>.*?<p.*?<p.*?>(.*?)</p>.*?<p.*?<p.*?>(.*?)</p>.*?<p.*?>(.*?)</p>.*?<p.*?<p.*?<p.*?<p.*?>(.*?)</p>.*?<p.*?<p.*?.*?>(.*?)</p>.*?</TR>", RegexOptions.Singleline);
        Regex regex_name = new Regex("<h4><font color=\"#9900FF\">.*?(.*?)</font>", RegexOptions.Singleline);
        ObservableCollection<Student> col = new ObservableCollection<Student>();
        ObservableCollection<Student> cope_col_T = new ObservableCollection<Student>();
        Calculate calculate = new Calculate();
        private static DispatcherTimer dispatcherTimer = new DispatcherTimer();
        private static DispatcherTimer dispatcherTimer1 = new DispatcherTimer();
        dawu book_dawu = new dawu();
        ObservableCollection<Is_Class_Book> is_Class_Book = new ObservableCollection<Is_Class_Book>();
        CookieContainer dawu_cookie = new CookieContainer();
        string book_result;
        string postdata1;
        string postdata2;
        int Left_Seconds = 0;

        WindowState ws;
        WindowState wsl;
        NotifyIcon notifyIcon;


        public MainWindow()
        {

            InitializeComponent();
            icon();
            wsl = WindowState;
            string update = GetHtmlData_Update("http://hitwh627.jd-app.com/update.ini");
            if ((update != "查询更新失败！")&&(update!="ver=2015.3.25"))
            {
                System.Windows.MessageBox.Show("发现新版本，请点击确定进行更新");
                System.Diagnostics.Process.Start("http://bbs.ghtt.net/thread-2008726-1-1.html");  
            }
            //保证窗体显示在上方。 
            foreach (var item in Enum.GetNames(typeof(dawu.Class_Name_Enum)))
            {
                is_Class_Book.Add(new Is_Class_Book { class_Name = item, isBook = true });
            }
            Class_Name_List.ItemsSource = is_Class_Book;

            week_combo.SelectedIndex = Properties.Settings.Default.week_save;
            day_combo.SelectedIndex = Properties.Settings.Default.day_save;
            time_combo.SelectedIndex = Properties.Settings.Default.class_time_save;
            stuid.Text = Properties.Settings.Default.stuid;
            pwd.Password = Properties.Settings.Default.score_pwd;
            dawu_name.Text = Properties.Settings.Default.name;
            dawu_pwd.Password = Properties.Settings.Default.book_pwd;
            time_span.Text = Properties.Settings.Default.time_span;
            this.DataContext = this;
        }

        private void icon()
        {
            this.notifyIcon = new NotifyIcon();
            this.notifyIcon.BalloonTipText = "哈工大学习小助手"; //设置程序启动时显示的文本
            this.notifyIcon.Text = "哈工大学习小助手";//最小化到托盘时，鼠标点击时显示的文本
            this.notifyIcon.Icon = new System.Drawing.Icon("ico.ico");//程序图标
            this.notifyIcon.Visible = true;
            notifyIcon.MouseDoubleClick += OnNotifyIconDoubleClick;
            this.notifyIcon.ShowBalloonTip(100);
        }

        private void OnNotifyIconDoubleClick(object sender, EventArgs e)
        {
            this.Show();
            WindowState = wsl;
        }

        private void Window_StateChanged(object sender, EventArgs e)
        {
            ws = WindowState;
            if (ws == WindowState.Minimized)
            {
                this.Hide();
            }
        }
    

        void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            book_result = dawu.AutoBook(dawu_cookie, postdata1, postdata2, is_Class_Book);
            Left_Seconds = Convert.ToInt32(time_span.Text);;
            if(book_result!="选课失败！")
            {
                dispatcherTimer.Stop();
                dispatcherTimer1.Stop();
                book_status_item.Content = book_result;
                this.notifyIcon.BalloonTipText = book_result;
                this.notifyIcon.ShowBalloonTip(100);
            }
        }

        void dispatcherTimer1_Tick(object sender, EventArgs e)
        {
            book_status_item.Content = "选课失败！将在  " + Convert.ToString(Left_Seconds) + "  秒后重试";
            Left_Seconds--;
        }

        ///<summary>
        ///打开默认浏览器
        ///</summary>


        /// <summary>
        /// 通过get方式请求页面，传递一个实例化的cookieContainer
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            ArrayList list = new ArrayList();
            request = WebRequest.Create(postUrl) as HttpWebRequest;
            request.Method = "GET";
            request.UserAgent = "Mozilla/4.0";
            request.CookieContainer = cookie;
            request.KeepAlive = true;
            //request.Timeout = 1000;
            request.CookieContainer = cookie;
            try
            {
                //获取服务器返回的资源
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
                    {
                        cookie.Add(response.Cookies);
                        //保存Cookies
                        list.Add(cookie);
                        list.Add(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {
                MessageBox.Show("教务处的服务器又崩啦/(ㄒoㄒ)/~~\n过会再试试看吧O(∩_∩)O嗯!");
                
                list.Clear();
                list.Add("发生异常/n/r");
                //WebResponse wr = ex.Response;
                //using (Stream st = wr.GetResponseStream())
                //{
                //    using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
                //    {
                //        list.Add(sr.ReadToEnd());
                //    }
                //}
            }
            catch (Exception ex)
            {
                list.Clear();
                list.Add("5");
                list.Add("发生异常：" + ex.Message);
            }
            return list;
        }


        public static string GetHtmlData_Update(string postUrl)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            CookieContainer update_cookie = new CookieContainer();
            string result;
            request = WebRequest.Create(postUrl) as HttpWebRequest;
            request.Method = "GET";
            request.UserAgent = "Mozilla/4.0";
            request.CookieContainer = update_cookie;
            request.KeepAlive = true;
            //request.Timeout = 1000;
            request.CookieContainer = update_cookie;
            try
            {
                //获取服务器返回的资源
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default))
                    {
                        result=reader.ReadToEnd();
                    }
                }
            }
            catch (WebException ex)
            {
                MessageBox.Show("查询更新失败！");
                result="查询更新失败！";
            }
            catch (Exception ex)
            {

               result=("发生异常：" + ex.Message);
            }
            return result;
        }


        /// <summary>
        /// 发送相关数据至页面
        /// 进行登录操作
        /// 并保存cookie
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="postUrl"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
        {
            ArrayList list = new ArrayList();
            HttpWebRequest request;
            HttpWebResponse response;
            ASCIIEncoding encoding = new ASCIIEncoding();
            request = WebRequest.Create(postUrl) as HttpWebRequest;

            byte[] b = encoding.GetBytes(postData);
            request.UserAgent = "Mozilla/4.0";
            request.Method = "POST";
            request.CookieContainer = cookie;
            request.ContentLength = b.Length;
            using (Stream stream = request.GetRequestStream())
            {
                stream.Write(b, 0, b.Length);
            }

            try
            {
                //获取服务器返回的资源
                using (response = request.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("GBK")))
                    {
                        if (response.Cookies.Count > 0)
                            cookie.Add(response.Cookies);
                        list.Add(cookie);
                        list.Add(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException wex)
            {
                WebResponse wr = wex.Response;
                using (Stream st = wr.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
                    {
                        list.Add(sr.ReadToEnd());
                    }
                }
            }
            catch (Exception ex)
            {
                list.Add("发生异常/n/r" + ex.Message);
            }

            return list;
        }

        private void get_score_button_Click(object sender, RoutedEventArgs e)
        {
            status_bar.Content = "正在尝试获取成绩......";
            col = new ObservableCollection<Student>();
            if (stuid.Text == "")
                MessageBox.Show("未输入学号！");
            else if (pwd.Password == "")
                MessageBox.Show("未输入密码！");
            else
            {
                CookieContainer cookie = new CookieContainer();
                ArrayList result;
                ArrayList arr_name;
                MatchCollection regex_result;
                MatchCollection regex_name_result;
                string postdata = "stuid=" + (string)stuid.Text + "&" + "pwd=" + (string)pwd.Password;
                if (GetHtmlData("http://222.194.15.1:7777/pls/wwwbks/bks_login2.login", cookie)[0] == "发生异常/n/r")
                {
                    status_bar.Content = "再试一次吧";
                    return;
                }
                
                arr_name = PostData(postdata, "http://222.194.15.1:7777/pls/wwwbks/bks_login2.login", cookie);
                result = GetHtmlData("http://222.194.15.1:7777/pls/wwwbks/bkscjcx.yxkc", cookie);
                regex_result = regex.Matches((string)result[1]);
                try
                {
                    regex_name_result = regex_name.Matches((string)arr_name[1]);
                    name_lable.Content = Convert.ToString(regex_name_result[0].Groups[1]);
                }
                catch (Exception)
                {

                    MessageBox.Show("童鞋你输入的学号或密码不对喔/(ㄒoㄒ)/~~");
                    status_bar.Content = "重新输入学号和密码";
                    return;
                    
                }

                foreach (Match match in regex_result)
                {
                    GroupCollection group = match.Groups;
                    col.Add(new Student() { Course = Convert.ToString(group[1]), Weight = Convert.ToString(group[2]), Score = Convert.ToString(group[4]), Type = Convert.ToString(group[5]), Time = Convert.ToString(group[3]), IsCalculate = true });
                }
                score_data_grid.ItemsSource = col;
            }
            status_bar.Content = "获取成功，点击计算看看(☆▽☆)";
        }

        private void cal_points_Click(object sender, RoutedEventArgs e)
        {
            if (col.Count==0)
            {
                MessageBox.Show("请先获取成绩");
                return;
            }
            status_bar.Content = "努力计算中...";
            int cal_type_status;                            //1计算所有，2计算考试课，3计算勾选的课程
            if (type_all_radio.IsChecked == true)
                cal_type_status = 1;
            else if (type_kaoshi_radio.IsChecked == true)
                cal_type_status = 2;
            else
                cal_type_status = 3;
            bool? is_all = true;
            is_all = type_all_radio.IsChecked;
            cope_col_T = new ObservableCollection<Student>();
            cope_col_T = calculate.copy_col(col);
            if (cal_type_status == 3)
            {
                if (calculate.check(cope_col_T) == true)
                {
                    std_points.Text = Convert.ToString(calculate.std_clc(cope_col_T, true));
                    beida_text.Text = Convert.ToString(calculate.beida_clc(cope_col_T, true));
                    canada_text.Text = Convert.ToString(calculate.canada_clc(cope_col_T, true));
                    shanghaijiaoda_text.Text = Convert.ToString(calculate.shanghaijiaoda_clc(cope_col_T, true));
                    gaijin1_text.Text = Convert.ToString(calculate.gaijin1_clc(cope_col_T, true));
                    gaijin2_text.Text = Convert.ToString(calculate.gaijin2_clc(cope_col_T, true));
                    baifenzhi_text.Text = Convert.ToString(calculate.baifenjiaquan_clc(cope_col_T, true));
                    sifenzhijiaquan_text.Text = Convert.ToString(calculate.sifenjiaquan_clc(cope_col_T, true));
                }
            }
            else
            {
                if (calculate.check(cope_col_T) == true)
                {
                    std_points.Text = Convert.ToString(calculate.std_clc(cope_col_T, is_all));
                    beida_text.Text = Convert.ToString(calculate.beida_clc(cope_col_T, is_all));
                    canada_text.Text = Convert.ToString(calculate.canada_clc(cope_col_T, is_all));
                    shanghaijiaoda_text.Text = Convert.ToString(calculate.shanghaijiaoda_clc(cope_col_T, is_all));
                    gaijin1_text.Text = Convert.ToString(calculate.gaijin1_clc(cope_col_T, is_all));
                    gaijin2_text.Text = Convert.ToString(calculate.gaijin2_clc(cope_col_T, is_all));
                    baifenzhi_text.Text = Convert.ToString(calculate.baifenjiaquan_clc(cope_col_T, is_all));
                    sifenzhijiaquan_text.Text = Convert.ToString(calculate.sifenjiaquan_clc(cope_col_T, is_all));
                }
            }
            if (Convert.ToDouble(std_points.Text) >= 3.5)
                status_bar.Content = "靠...活捉学霸一只";
            else
                status_bar.Content = "还不赶紧关电脑好好自习去~~~^_^~~~";
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("    标准4.0\n成绩	GPA\n100～90	4.0\n89～80	3.0\n79～70	2.0\n69～60	1.0\n59～0	0");
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("   北大4.0\n成绩	GPA\n100～90	4.0\n89～85	3.7\n84～82	3.3\n81～78	3.0\n77～75	2.7\n74～72	2.3\n71～68	2.0\n67～64	1.5\n63～60	1.0\n59～0	0");
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("   加拿大4.3\n成绩	学分\n100～90	4.3\n89～85	4.0\n84～80	3.7\n79～75	3.3\n74～70	3.0\n69～65	2.7\n64～60	2.3\n59～0	0");
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            MessageBox.Show(" 上海交大4.3\n成绩	学分\n100～95	4.3\n94～90	4.0\n89～85	3.7\n84～80	3.3\n79～75	3.0\n74～70	2.7\n69～67	2.3\n66～65	2.0\n64～62	1.7\n61～60	1.0\n59～0	0");
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("   改进4.0(1)\n成绩	GPA\n100～85	4.0\n84～70	3.0\n69～60	2.0\n59～0	0");
        }

        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("   改进4.0(2)\n成绩	GPA\n100～85	4.0\n84～75	3.0\n74～60	2.0\n59～0	0");
        }

        private void Button_Click_6(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("GPA=∑(成绩*学分)/∑学分");
        }

        private void Button_Click_7(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("GPA=∑(成绩*学分)/∑学分*4/100");
        }

        private void type_kaoshi_radio_Checked(object sender, RoutedEventArgs e)
        {
            foreach (Student item in col)
            {
                if (item.Type == "考查" || item.Type == "")
                    item.IsCalculate = false;
            }
            score_data_grid.ItemsSource = col;
        }

        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Application.Current.Shutdown();
            //Application.Current.Shutdown();
        }

        private void MenuItem_Click_1(object sender, RoutedEventArgs e)
        {
            var dialog = new About();
            dialog.ShowDialog();
        }

        private void login_button_Click(object sender, RoutedEventArgs e)
        {
            if (dawu_name.Text == "")
                MessageBox.Show("未输入学号！");
            else if (dawu_pwd.Password == "")
                MessageBox.Show("未输入密码！");
            string postdata;
            ArrayList login;
            string login_str;
            postdata = "name=" + dawu_name.Text + "&" + "pwd=" + dawu_pwd.Password;
            login = dawu.PostData(postdata, "http://172.31.65.65/booking/css/forlogin.aspx", dawu_cookie);
            if ((string)login[0] == "无法连接到远程服务器")
                return;
            login_str = (string)login[0];
            if (login_str == "yes")
            {
                this.notifyIcon.BalloonTipText = "登陆成功！";
                book_status_item.Content = "登陆成功！";
                this.notifyIcon.ShowBalloonTip(100);

            }
            else
                MessageBox.Show("登录失败！");

        }

        private void book_button_Click(object sender, RoutedEventArgs e)
        {
            postdata1 = "action=booking_add&stu_number=" + dawu_name.Text + "&exp_number=";
            postdata2 = "&exp_week=" + week_combo.SelectedItem.ToString() + "&exp_weekday=" + day_combo.SelectedItem.ToString() + "&exp_class=" + time_combo.SelectedItem.ToString();
            book_result = dawu.AutoBook(dawu_cookie, postdata1, postdata2, is_Class_Book);
            dispatcherTimer.Tick += dispatcherTimer_Tick;
            Left_Seconds = Convert.ToInt32(time_span.Text);
            dispatcherTimer.Interval = new TimeSpan(0, 0, Left_Seconds);

            if (book_result == "选课失败！")
            {
                dispatcherTimer.Start();
                dispatcherTimer1.Tick += dispatcherTimer1_Tick;
                dispatcherTimer1.Interval = new TimeSpan(0, 0, 1);
                dispatcherTimer1.Start();
                //book_status_item.Content = "选课失败，将于3分钟后重试";
            }
            else
            {

                book_status_item.Content = book_result;
                this.notifyIcon.BalloonTipText = book_result;
                this.notifyIcon.ShowBalloonTip(100);

            }
            //int index_exp = 1;
            //string result_str = null;
            //string postdata;
            //while (result_str != "yes选课成功")
            //{
            //    postdata = "action=booking_add&stu_number=" + dawu_name.Text + "&exp_number=" + Convert.ToSingle(index_exp) + "&exp_week=" + week_combo.SelectedItem.ToString() + "&exp_weekday=" + day_combo.SelectedItem.ToString() + "&exp_class=" + time_combo.SelectedItem.ToString();
            //    result_str = (string)dawu.PostData(postdata, "http://172.31.65.65/booking/forajax.aspx", dawu_cookie)[1];
            //    index_exp = index_exp + 1;
            //    if (index_exp >= 13)
            //    {
            //        MessageBox.Show("选课失败！");
            //        return;
            //    }
            //}
            //if  (index_exp-1==1)
            //    MessageBox.Show("选课成功！\n 全息照相(N126)");
            //if (index_exp - 1 == 2)
            //    MessageBox.Show("选课成功！\n 太阳能电池(N134)");
            //if (index_exp - 1 == 3)
            //    MessageBox.Show("选课成功！\n 线阵CCD(N135)");
            //if (index_exp - 1 == 4)
            //    MessageBox.Show("选课成功！\n 金属电子逸出功(N141)");
            //if (index_exp - 1 == 5)
            //    MessageBox.Show("选课成功！\n 光电效应(N141)");
            //if (index_exp - 1 == 6)
            //    MessageBox.Show("选课成功！\n RC电路的研究(N144)");
            //if (index_exp - 1 == 7)
            //    MessageBox.Show("选课成功！\n 动态磁滞回线(N144)");
            //if (index_exp - 1 == 8)
            //    MessageBox.Show("选课成功！\n 迈克尔逊干涉仪(N149)");
            //if (index_exp - 1 == 9)
            //    MessageBox.Show("选课成功！\n 分光计(N145)");
            //if (index_exp - 1 == 10)
            //    MessageBox.Show("选课成功！\n 偏振光(N148)");
            //if (index_exp - 1 == 11)
            //    MessageBox.Show("选课成功！\n 声速的测定(N148)");
        }

        private void quit_button_Click(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.week_save = week_combo.SelectedIndex;
            Properties.Settings.Default.day_save = day_combo.SelectedIndex;
            Properties.Settings.Default.class_time_save = time_combo.SelectedIndex;
            Properties.Settings.Default.stuid = stuid.Text;
            Properties.Settings.Default.score_pwd = pwd.Password;
            Properties.Settings.Default.name = dawu_name.Text;
            Properties.Settings.Default.book_pwd = dawu_pwd.Password;
            Properties.Settings.Default.time_span = time_span.Text;
            Properties.Settings.Default.Save();

            System.Windows.Application.Current.Shutdown();
        }



    }
    class Student
    {
        private string _course;
        private string _weight;
        private string _score;
        private string _type;
        private string _time;
        private bool _isCalculate = true;
        public string Course
        {
            set { _course = value; }
            get { return _course; }
        }
        public string Weight
        {
            set { _weight = value; }
            get { return _weight; }
        }
        public string Score
        {
            set { _score = value; }
            get { return _score; }
        }
        public string Type
        {
            set { _type = value; }
            get { return _type; }
        }
        public string Time
        {
            set { _time = value; }
            get { return _time; }
        }
        public bool IsCalculate
        {
            set { _isCalculate = value; }
            get { return _isCalculate; }
        }
    }

    class Calculate
    {
        public double std_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    if (Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 80)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 79 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 80)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 79 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }

            return Math.Round(point / weight, 2);
        }
        public double gaijin1_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    if (Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            return Math.Round(point / weight, 2);
        }
        public double gaijin2_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    if (Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            return Math.Round(point / weight, 2);
        }
        public double beida_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    if (Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 82)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 81 && Convert.ToDouble(item.Score) >= 78)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 77 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 72)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 71 && Convert.ToDouble(item.Score) >= 68)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 67 && Convert.ToDouble(item.Score) >= 64)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.5;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 63 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 82)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 81 && Convert.ToDouble(item.Score) >= 78)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 77 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 72)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 71 && Convert.ToDouble(item.Score) >= 68)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 67 && Convert.ToDouble(item.Score) >= 64)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.5;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 63 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            return Math.Round(point / weight, 2);
        }
        public double canada_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    if (Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 80)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 79 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 65)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 64 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 80)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 79 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 65)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 64 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }

            }
            return Math.Round(point / weight, 2);
        }
        public double shanghaijiaoda_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    if (Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 95)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 94 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 80)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 79 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 67)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 66 && Convert.ToDouble(item.Score) >= 65)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 64 && Convert.ToDouble(item.Score) >= 62)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToDouble(item.Score) <= 61 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 100 && Convert.ToDouble(item.Score) >= 95)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 94 && Convert.ToDouble(item.Score) >= 90)
                    {
                        point += Convert.ToDouble(item.Weight) * 4.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 89 && Convert.ToDouble(item.Score) >= 85)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 84 && Convert.ToDouble(item.Score) >= 80)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 79 && Convert.ToDouble(item.Score) >= 75)
                    {
                        point += Convert.ToDouble(item.Weight) * 3.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 74 && Convert.ToDouble(item.Score) >= 70)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 69 && Convert.ToDouble(item.Score) >= 67)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.3;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 66 && Convert.ToDouble(item.Score) >= 65)
                    {
                        point += Convert.ToDouble(item.Weight) * 2.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 64 && Convert.ToDouble(item.Score) >= 62)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.7;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试" && Convert.ToDouble(item.Score) <= 61 && Convert.ToDouble(item.Score) >= 60)
                    {
                        point += Convert.ToDouble(item.Weight) * 1.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                    else if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Weight) * 0.0;
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            return Math.Round(point / weight, 2);
        }
        public double baifenjiaquan_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    point += Convert.ToDouble(item.Score) * Convert.ToDouble(item.Weight);
                    weight += Convert.ToDouble(item.Weight);
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Score) * Convert.ToDouble(item.Weight);
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            return Math.Round(point / weight, 2);
        }
        public double sifenjiaquan_clc(ObservableCollection<Student> col, bool? is_all)
        {
            double point = 0.0;
            double weight = 0.0;
            if (is_all == true)
            {
                foreach (Student item in col)
                {
                    point += Convert.ToDouble(item.Score) * Convert.ToDouble(item.Weight);
                    weight += Convert.ToDouble(item.Weight);
                }
            }
            else
            {
                foreach (Student item in col)
                {
                    if (Convert.ToString(item.Type) == "考试")
                    {
                        point += Convert.ToDouble(item.Score) * Convert.ToDouble(item.Weight);
                        weight += Convert.ToDouble(item.Weight);
                    }
                }
            }
            return Math.Round((point / weight * 4) / 100, 2);
        }
        public bool check(ObservableCollection<Student> col)
        {

            foreach (Student item in col)
            {
                if (item.Score == "" || item.Weight == "")
                {
                    MessageBox.Show("获取的成绩或学分中存在空白项，请将空白项修改为确定数字或取消勾选那项后重新计算！");
                    return false;
                }
            }
            return true;
        }
        public ObservableCollection<Student> copy_col(ObservableCollection<Student> col)
        {
            ObservableCollection<Student> copy_one = new ObservableCollection<Student>();
            foreach (Student item in col)
            {
                if (item.IsCalculate == true)
                {
                    copy_one.Add(item);
                }
            }
            return copy_one;
        }
    }


}
